/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Manish Mehta 
 												
Class							:				BOFixes
 
Function						: 				A class for holding fixes for BasicOperators
****************************************************************************/
package behavioradaptation.fixes;


import java.util.List;

import cbr.episodicCBR.EpisodicCBR;

import planninglanguage.plans.Plan;
import trace.Entry;
import trace.Trace;
import behavioradaptation.analysisdata.AbstractedTrace;
import behavioradaptation.analysisdata.FailureTypes;
import behavioradaptation.analysisdata.PlanFailuresData;
import behavioradaptation.reasoner.TraceHelpers;

public class BOFixes {
	public static void findFixedBOPlans(List<PlanFailuresData> plfailures, AbstractedTrace a_abstractedtrace, EpisodicCBR a_cbr)
	{
		Trace a_trace = a_abstractedtrace.getTrace();
		for(int i=0; i < plfailures.size(); i++)
		{
			PlanFailuresData p = (PlanFailuresData)plfailures.get(i);
			List<FailureTypes.m_failuretype> fixes = p.getFailures();
	
			Plan tobefixedplan = (Plan) p.getPlan();	
			Plan fixedplan = null;
			for(int k=0; k < fixes.size(); k++)
			{
				fixedplan = null;
				FailureTypes.m_failuretype f= (FailureTypes.m_failuretype)fixes.get(k);
				// Find the fix for each of the basic operator failures
				if(f == FailureTypes.m_failuretype.NOT_ENOUGH_FOOD)	
					fixedplan = NotEnoughFoodBOFix.fix(tobefixedplan, p.getGameState());
				else if(f == FailureTypes.m_failuretype.NOT_ENOUGH_GOLD)
					fixedplan = NotEnoughGoldBOFix.fix(tobefixedplan, p.getGameState());
				else if(f == FailureTypes.m_failuretype.NOT_ENOUGH_WOOD)
					fixedplan = NotEnoughWoodBOFix.fix(tobefixedplan, p.getGameState());
				else if(f == FailureTypes.m_failuretype.NOT_ENOUGH_OIL)
					fixedplan = NotEnoughOilBOFix.fix(tobefixedplan, p.getGameState());
				else if(f == FailureTypes.m_failuretype.NO_PEASANT)
					fixedplan = NoPeasantBOFix.fix(tobefixedplan, p.getGameState());	
				if(fixedplan != null)
				{	
					Entry entry = (Entry) a_trace.get(p.getIndex());
					// Modify the Case base and add the modified plan to it 
					TraceHelpers.modifyCaseBase(tobefixedplan, a_trace, fixedplan, entry, true, a_cbr);
				}
			}
		}		
	}



}
